package net.xdclass.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.xdclass.component.MinioService;
import net.xdclass.enums.BizCodeEnum;
import net.xdclass.enums.SendCodeEnum;
import net.xdclass.mapper.UserMapper;
import net.xdclass.model.UserDO;
import net.xdclass.request.UserLoginRequest;
import net.xdclass.request.UserRegisterRequest;
import net.xdclass.service.NotifyService;
import net.xdclass.service.UserService;
import net.xdclass.util.CommonUtil;
import net.xdclass.util.JsonData;
import net.xdclass.vo.UserVo;
import org.apache.commons.codec.digest.Md5Crypt;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author JZ
 * @since 2023-04-26
 */
@Api(tags = "用户模块")
@RestController
@RequestMapping("/api/user/v1")
@Slf4j
public class UserController {

    @Resource
    private MinioService minioService;

    @Resource
    private NotifyService notifyService;

    @Resource
    private UserMapper userMapper;

    @Resource
    private UserService userService;

    /**
     * @Description: 用户头像上传
     * 默认大小是1m
     * @Author: jz
     * @Date: 2023/5/1 14:26
     * @Param: file 文件
     * @Return:
     */
    @ApiOperation("用户头像上传")
    @PostMapping("/upload")
    public JsonData uploadImg(@RequestPart("file") MultipartFile file) {

        String url = minioService.upload(file, "xdclass", file.getOriginalFilename(), "user");

        return url != null ? JsonData.buildSuccess(url) : JsonData.buildSuccess(BizCodeEnum.FILE_UPLOAD_USER_IMG_FAIL);
    }

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public JsonData register(@RequestBody UserRegisterRequest userRegisterRequest) {
        //校验验证码
        boolean bool = notifyService.checkCode(SendCodeEnum.USER_REGISTER, userRegisterRequest.getMail(), userRegisterRequest.getCode());
        if (!bool) {
            return JsonData.buildResult(BizCodeEnum.CODE_ERROR);
        }
        UserDO userDO = new UserDO();
        BeanUtils.copyProperties(userRegisterRequest, userDO);
        userDO.setCreateTime(new Date());
        //密码加密
        userDO.setSecret("$1$" + CommonUtil.getStringNumRandom(8));
        //密码+盐
        String cryptPwd = Md5Crypt.md5Crypt(userRegisterRequest.getPwd().getBytes(), userDO.getSecret());
        userDO.setPwd(cryptPwd);
        //账号唯一性检查 // TODO: 2023/5/1
        if (checkUnique(userDO.getMail())) {
            int rows = userMapper.insert(userDO);
            log.info("rows:{},注册成功:{}", rows, userDO.toString());
            //新用户注册成功发放福利
            userRegisterInitTask(userDO);
        } else {
            return JsonData.buildResult(BizCodeEnum.ACCOUNT_REPEAT);
        }

        return null;
    }

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public JsonData login(@RequestBody UserLoginRequest userLoginRequest) {
        JsonData jsonData = userService.login(userLoginRequest);
        return jsonData;
    }

    @ApiOperation("用户信息")
    @GetMapping("/detail")
    public JsonData detail() {
        UserVo userVo = userService.findUserDetail();
        return JsonData.buildSuccess(userVo);
    }

    private boolean checkUnique(String mail) {
        QueryWrapper<UserDO> queryWrapper = new QueryWrapper<UserDO>().eq("mail", mail);
        List<UserDO> userDOS = userMapper.selectList(queryWrapper);
        if (userDOS.size() > 0) {
            return true;
        }
        return false;
    }

    /**
     * @Description: 用户初始化福利信息 todo
     * @Author: jz
     * @Date: 2023/5/1 18:27
     * @Param:
     * @Return:
     */
    private void userRegisterInitTask(UserDO userDO) {

    }
}

